package com.uenpay.im.ui.old.activity.red;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.ZProgressHUD;
import com.uenpay.util.app.ActivityStack;
import com.uenpay.util.rx.RxBus;

/**
 * @author SL
 */
public class ReceiveRedDialogActivity extends AppCompatActivity {

    private TextView received_red_btn, name_tv, content_tv, send_red_tv,
            memo_tv;
    private RoundedImageView user_iv;
    private ReceiveRedEntity receiveRedEntity = null;
    private RedHandler mRedHandler = new RedHandler();
    private ZProgressHUD zProgressHUD = null;
    private View loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_receive_red_dialog_layout);
        ActivityStack.getInstance().push(this);
        receiveRedEntity = (ReceiveRedEntity) getIntent().getSerializableExtra(
                "mReceiveRedEntity");
        initView();
        initListener();
        if (receiveRedEntity != null) {
            initData();
        }
    }


    protected void initView() {
        zProgressHUD = ZProgressHUD.getInstance(this);
        name_tv = (TextView) findViewById(R.id.name_tv);
        content_tv = (TextView) findViewById(R.id.content_tv);
        user_iv = (RoundedImageView) findViewById(R.id.user_iv);
        send_red_tv = (TextView) findViewById(R.id.send_red_tv);
        memo_tv = (TextView) findViewById(R.id.memo_tv);
        received_red_btn = (TextView) findViewById(R.id.received_red_btn);
        loading = findViewById(R.id.tran_loading);
    }

    protected void initListener() {
        findViewById(R.id.close_iv).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        received_red_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                received_red_btn.setEnabled(false);
                zProgressHUD.show();
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        openRed();
                    }
                }, 500);

            }
        });
        memo_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (receiveRedEntity.tcustId != 0
                        && receiveRedEntity.chatid == 0) {
                    Intent intent = new Intent(ReceiveRedDialogActivity.this,
                            RedDetailPersonalActivity.class);
                    intent.putExtra("mReceiveRedEntity",
                            receiveRedEntity);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(ReceiveRedDialogActivity.this,
                            RedDetailActivity.class);
                    intent.putExtra("mReceiveRedEntity",
                            receiveRedEntity);
                    startActivity(intent);
                }
                finish();
            }
        });
    }

    private void initData() {
        user_iv.SetUrl(APIConfiguration.getAvatarUrlNormal(receiveRedEntity.custId, PacketType.PERSONAL));
        name_tv.setText(receiveRedEntity.nickName);
        if (receiveRedEntity.expired == 0 && receiveRedEntity.isOpened == 0 && receiveRedEntity.leftPacketsNum > 0) {
            if (TextUtils.isEmpty(receiveRedEntity.blessing)) {
                content_tv.setText(R.string.red_default_content);
            } else {
                content_tv.setText(receiveRedEntity.blessing);
            }
        } else {
            if (receiveRedEntity.expired == 1) {
                send_red_tv.setText(getResources().getString(
                        R.string.red_expired));
            } else {
                send_red_tv
                        .setText(getResources().getString(R.string.red_over));
            }
            if (TextUtils.isEmpty(receiveRedEntity.blessing)) {
                content_tv.setText(R.string.red_default_content);
            } else {
                content_tv.setText(receiveRedEntity.blessing);
            }
            received_red_btn.setVisibility(View.INVISIBLE);
            memo_tv.setVisibility(View.VISIBLE);
        }
    }

    private void openRed() {
        String packetsid = receiveRedEntity.packetsid;
        if (TextUtils.isEmpty(packetsid)) {
            return;
        }
        mRedHandler.openRed(packetsid, new RedHandler.IOpenRedListener() {

            @Override
            public void onOpenRed(int status, String msg,
                                  ReceiveRedEntity mReceiveRedEntity) {
                received_red_btn.setEnabled(true);
                zProgressHUD.dismiss();
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    if (mReceiveRedEntity.tcustId != 0 && mReceiveRedEntity.chatid == 0) {
                        Intent intent = new Intent(ReceiveRedDialogActivity.this,
                                RedDetailPersonalActivity.class);
                        intent.putExtra("mReceiveRedEntity",
                                mReceiveRedEntity);
                        startActivity(intent);
                        finish();
                    } else {
                        Intent intent = new Intent(ReceiveRedDialogActivity.this,
                                RedDetailActivity.class);
                        intent.putExtra("mReceiveRedEntity", mReceiveRedEntity);
                        startActivity(intent);
                        finish();
                    }

                } else {
                    IMApplication.showToast(msg);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.$().unregister(this);
        ActivityStack.getInstance().remove(this);
    }
}
